import unsafe

fn ascii(string c):u8 {
    if c.len() == 0 {
        return 0
    }
    var list = c as [u8]
    return list[0]
}

// 引用传递
fn reverse<T>([T] list) {
    int start = 0
    int end = list.len() - 1
    for start < end {
        (list[start], list[end]) = (list[end], list[start])
        start += 1
        end -= 1
    }
}

fn utos<T>(anyptr v):string {
    return utos_with<T>(v, '0', 0 as u8)
}

// 十六进制转换函数（小写）
fn utos_hex_with<T>(anyptr v, string fill, u8 width):string {
    var i = unsafe.ptr_to<T>(&v as anyptr)

    T base = 16
    var result = vec_new<u8>(0, 0)

    if i == 0 {
        if width == 0 {
            return '0'
        }

        // 填充 width 个数的 fill 字符，并返回
        for u8 j = 0; j < width; j+=1 {
            result.push(ascii(fill))
        }

        return result as string
    }

    // 按 base 进行转换
    for i != 0 {
        T rem = i % base
        if rem < 10 {
            result.push(rem as u8 + ascii('0'))
        } else {
            result.push((rem - 10) as u8 + ascii('a'))
        }

        i = i / base
    }

    // 填充字符
    u8 result_width = result.len() as u8
    for width > result_width {
       result.push(ascii(fill))        
       result_width += 1
    }

    // 数组反转
    reverse(result)

    return result as string
}

fn utos_hex<T>(anyptr v):string {
    return utos_hex_with<T>(v, '0', 0 as u8)
}

// 十六进制转换函数（大写）
fn utos_hex_upper_with<T>(anyptr v, string fill, u8 width):string {
    var i = unsafe.ptr_to<T>(&v as anyptr)

    T base = 16
    var result = vec_new<u8>(0, 0)

    if i == 0 {
        if width == 0 {
            return '0'
        }

        // 填充 width 个数的 fill 字符，并返回
        for u8 j = 0; j < width; j+=1 {
            result.push(ascii(fill))
        }

        return result as string
    }

    // 按 base 进行转换
    for i != 0 {
        T rem = i % base
        if rem < 10 {
            result.push(rem as u8 + ascii('0'))
        } else {
            result.push((rem - 10) as u8 + ascii('A'))
        }

        i = i / base
    }

    // 填充字符
    u8 result_width = result.len() as u8
    for width > result_width {
       result.push(ascii(fill))        
       result_width += 1
    }

    // 数组反转
    reverse(result)

    return result as string
}

fn utos_hex_upper<T>(anyptr v):string {
    return utos_hex_upper_with<T>(v, '0', 0 as u8)
}

fn utos_with<T>(anyptr v, string fill, u8 width):string {
    var i = unsafe.ptr_to<T>(&v as anyptr)

    T base = 10
    var result = vec_new<u8>(0, 0)

    if i == 0 {
        if width == 0 {
            return '0'
        }

        // 填充 width 个数的 fill 字符，并返回
        for u8 j = 0; j < width; j+=1 {
            result.push(ascii(fill))
        }

        return result as string
    }

    // 按 base 进行转换
    for i != 0 {
        T rem = i % base
        result.push(rem as u8 + ascii('0'))

        i = i / base
    }

    // 填充字符
    u8 result_width = result.len() as u8
    for width > result_width {
       result.push(ascii(fill))        
       result_width += 1
    }

    // 数组反转
    reverse(result)

    return result as string
}

fn itos<T>(anyptr v):string {
    return itos_with<T>(v, '0', 0 as u8)
}

fn itos_with<T>(anyptr v, string fill, u8 width):string {
    var i = unsafe.ptr_to<T>(&v as anyptr)

    var base = 10
    var is_neg = false

    if i < 0 {
        is_neg = true
        i = -i // 转换为正数
    }

    var result = vec_new<u8>(0, 0)

    if i == 0 {
        if width == 0 {
            return '0'
        }

        // 填充 width 个数的 0， 并返回
        for u8 i = 0; i < width; i+=1 {
            result.push(ascii(fill))
        }

        return result as string
    }


    // 按 base 进行转换
    for i != 0 {
        int rem = i as int % base
        result.push(rem as u8 + ascii('0') as u8)

        i = i / 10
    }

    u8 result_width = result.len() as u8
    if is_neg {
        result_width += 1
    }

    if fill == ' ' {
        if is_neg {
            result.push(ascii('-'))
        }
        // 填充 ' '
        for width > result_width {
           result.push(ascii(fill))        
           result_width += 1
        } 
    } else {
        // fill == '0'
        for width > result_width {
           result.push(ascii(fill))        
           result_width += 1
        }

        if is_neg {
            result.push(ascii('-'))
        }
    }

    // 数组反转
    reverse(result)

    return result as string
}

fn ftos<T>(anyptr v):string {
    return ftos_with<T>(v, '0', 0 as u8, 6 as u8)
}

fn ftos_with<T>(anyptr v, string fill, u8 width, u8 precision):string {
    var f = unsafe.ptr_to<T>(&v as anyptr)

    var is_neg = false
    if f < 0 {
        is_neg = true
        f = -f
    }

    // 取整数部分
    int int_part = f as int
    var int_str = itos<int>(int_part as anyptr)  // 将整数部分转换为字符串

    // 取小数部分
    var frac_part = f - int_part as T
    var frac_str = ''

    if frac_part > 0 && precision > 0 {
        frac_str = '.'
        
        // 将 frac_part 转换成整数(precision 位数)
        for int i = 0; i < precision as int; i+=1 {
            var digit = (frac_part * 10) as int
            frac_str += itos<int>(digit as anyptr)

            frac_part = (frac_part * 10) - digit as T
        }
    }

    var s = ''
    if is_neg {
        s = '-' + int_str + frac_str
    } else {
        s = int_str + frac_str
    }
    
    if s.len() >= width as int {
        return s
    }
    
    var padding = width as int - s.len()
    var result = vec_new<u8>(0, 0)
    
    // 处理负数情况
    if is_neg {
        if fill == ' ' {
            // 对于空格填充的负数，先添加符号再填充
            result.push(ascii('-'))
            s = s.slice(1, s.len())  // 移除负号
            
            // 添加填充字符
            for int i = 0; i < padding; i += 1 {
                result.push(ascii(fill))
            }
        } else {
            // 对于0填充的负数，先添加符号，再填充0
            result.push(ascii('-'))
            s = s.slice(1, s.len())  // 移除负号
            
            // 添加填充字符
            for int i = 0; i < padding; i += 1 {
                result.push(ascii(fill))
            }
        }
    } else {
        // 对于正数，先填充再添加数字
        // 添加填充字符
        for int i = 0; i < padding; i += 1 {
            result.push(ascii(fill))
        }
    }
    
    // 添加数字字符串
    for c in s {
        result.push(c)
    }
    
    return result as string
}